import { Injectable } from '@angular/core';

export enum ComponentViewEnm {
  MANAGER = 'ManagerComponent',
  ARTICLECREATE = 'ArticleCreateComponent',
  NULL = ''
}

@Injectable()
export class ComponetViewManagerService {
  private views: Array<ComponentViewEnm> = [];
  constructor() { }

  public getCurrentView(): ComponentViewEnm {
    return this.getLastViews();
  }

  public setCurrentView(v: ComponentViewEnm): void {
    this.views.push(v);
  }

  public destroyCurrentView(v: ComponentViewEnm): void {
    if (v === this.getLastViews()) {
      this.views.pop();
      return;
    }
    this.views.splice(this.getViewIndex(v));
  }

  private getLastViews(): ComponentViewEnm {
    const len: number = this.views.length;
    if (len) {
      return this.views[len - 1];
    }
    return ComponentViewEnm.NULL;
  }

  private getViewIndex(v: ComponentViewEnm ): number {
    const views = this.views;
    for (let i = 0, len = views.length; i < len; i++) {
      if (views[i] === v) {
        return i;
      }
    }
    return -1;
  }
}
